package com.webflux.demo.webfluxdemo.filter;

import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Component
public class MyWebFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        System.out.println("请求处理放行之前");

        Mono<Void> mono = chain
                .filter(exchange) // 放行
                .doOnError(err -> {
                    System.out.println("发生异常");
                })
                .doFinally(signalType -> {
                    System.out.println("真正的请求处理放行之后");
                });
        System.out.println("不是请求处理放行之后");
        return mono;
    }
}
